<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="系统名称" prop="systemName">
        <el-select
          v-model="queryParams.systemName"
          placeholder="请选择系统名称"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in systemOptions"
            :key="dict.sysNum"
            :label="dict.sysName"
            :value="dict.sysNum"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="账号" prop="account">
        <el-input
          v-model="queryParams.account"
          placeholder="请输入账号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
    
      <el-form-item label="持有人" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入持有人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="账号状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择账号状态"
          clearable
          size="small"
        >
          <el-option label="启用" value="0" />
          <el-option label="停用" value="1" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="cyan"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:integ:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:integ:remove']"
          >删除</el-button
        >
      </el-col>
      <!--<el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:integ:export']"
        >导出</el-button>
      </el-col>-->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="integList"
      @selection-change="handleSelectionChange"
      :default-sort="{ prop: 'createTime', order: 'descending' }"
      @sort-change="tableSortChange"
    >
      <el-table-column type="selection" width="55" align="center" />
       <el-table-column
        label="系统名称"
        align="center"
        prop="systemName"
        :sort-orders="['ascending', 'descending']"
        :formatter="systemNameFormatter"
        sortable="custom"
      />
      <el-table-column
        label="账号"
        align="center"
        prop="account"
        :sort-orders="['ascending', 'descending']"
        sortable="custom"
      />
       <el-table-column label="持有人" align="center" prop="userName" />
      <el-table-column
        label="状态"
        align="center"
        prop="status"
        :formatter="statusFormat" 
        :sort-orders="['ascending', 'descending']"
        sortable="custom"
      />
       <!-- <el-table-column
        label="开通申请单号"
        align="center"
        prop="applyNo"
        :sort-orders="['ascending', 'descending']"
        sortable="custom"
      />
       <el-table-column
        label="注销申请单号"
        align="center"
        prop="cancelNo"
        :sort-orders="['ascending', 'descending']"
        sortable="custom"
      /> -->
       <el-table-column
        label="开通日期"
        align="center"
        prop="openTime"
        :sort-orders="['ascending', 'descending']"
        sortable="custom"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.openTime, '{y}-{m}-{d}') }}</span>
        </template>
       </el-table-column>
       <el-table-column
        label="注销日期"
        align="center"
        prop="cancelTime"
        :sort-orders="['ascending', 'descending']"
        sortable="custom"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.cancelTime, '{y}-{m}-{d}') }}</span>
        </template>
       </el-table-column>

        <!-- <el-table-column
        label="是否删除"
        align="center"
        prop="flag"
        :formatter="flagFormat" 
        :sort-orders="['ascending', 'descending']"
        sortable="custom"
      /> -->
      <el-table-column
        label="备注"
        align="center"
        prop="remark"
        :sort-orders="['ascending', 'descending']"
        sortable="custom"
      />
     
     
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:integ:edit']"
            >修改</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改后台集成用户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="98px">
        <el-form-item label="系统名称" prop="systemName">
          <el-select v-model="form.systemName" placeholder="请选择系统名称">
            <el-option
              v-for="dict in systemOptions"
              :key="dict.sysNum"
              :label="dict.sysName"
              :value="dict.sysNum"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="账号" prop="account">
          <el-input v-model="form.account" placeholder="请输入账号" />
        </el-form-item>
        <el-form-item label="持有人" prop="userName">
          <el-input
            v-model="form.userName"
            readonly="readonly"
            @click.native="showChooseUser"
            placeholder="请选择持有人"
          />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态" clearable size="small" style="width: 240px">
            <el-option label="启用" value="0" />
            <el-option label="停用" value="1" />
        </el-select>
        </el-form-item>
        <el-form-item label="开通日期" prop="openTime">
           <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.openTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择开通日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="注销日期" prop="cancelTime">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.cancelTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择注销日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="开通申请单号" prop="applyNo">
          <el-input v-model="form.applyNo" placeholder="请输入开通申请单号" />
        </el-form-item>
         <el-form-item label="注销申请单号" prop="cancelNo">
          <el-input v-model="form.cancelNo" placeholder="注销申请单号" />
        </el-form-item>
        <!-- <el-form-item label="是否删除" prop="flag">
          <el-select v-model="form.flag" placeholder="请选择是否删除" clearable size="small" style="width: 240px">
            <el-option label="是" value="0" />
            <el-option label="否" value="1" />
        </el-select>
        </el-form-item> -->
        <el-form-item label="持有人id" prop="userId" v-show="false">
          <el-input v-model="form.userId" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <choose-user
      :dialogVisible="tabdialogVisible"
      :userId="form.userId"
      :userName="form.userName"
      @changetabdialogVisible="changetabdialogVisible"
      @chooseUserNames="chooseUserNames"
      @chooseUserIds="chooseUserIds"
      ref="chooseUserData"
    >
    </choose-user>
  </div>
</template>

<script>
import {
  listInteg,
  getInteg,
  delInteg,
  addInteg,
  updateInteg,
  exportInteg,
} from "@/api/system/integ";
import { getSystemDictInteg } from "@/api/system/sysList";
import { findOrgUserTree } from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import chooseUser from "@/components/ChooseUser";
export default {
  name: "Integ",
  components: { Treeselect, chooseUser },
  data() {
    return {
      userId: "",
      userName: "",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      accounts: [],
      // 非单个禁用
      single: true,
      persons: [],
      // 非多个禁用
      multiple: true,
      tabdialogVisible: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 后台集成用户表格数据
      integList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      systemOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        account: null,
        systemName: null,
        userName: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        systemName: [
          { required: true, message: "系统名称不能为空", trigger: "change" },
        ],
        account: [{ required: true, message: "账号不能为空", trigger: "blur" }],
      },
    };
  },
  mounted: function () {
    this.getPersons();
  },
  created() {
    this.getSystemOptions();

    this.getList();
  },
  methods: {
    showChooseUser() {
      debugger
      this.tabdialogVisible = true;
      this.$nextTick(() => {
        this.$refs.chooseUserData.checkRow();
      });
    },
    changetabdialogVisible(val) {
      this.tabdialogVisible = val;
    },
    chooseUserNames(names) {
      this.form.userName = names
    },
    chooseUserIds(ids) {
      this.form.userId = ids.join(";");
    },
    systemNameFormatter(row, column) {
      var actions = [];
      var datas = this.systemOptions,
        value = row.systemName;
      Object.keys(datas).some((key) => {
        if (datas[key].sysNum == "" + value) {
          actions.push(datas[key].sysName);
          return true;
        }
      });
      return actions.join("");
    },
    getPersons() {
      findOrgUserTree().then((res) => {
        this.persons = res.data;
      });
    },
    getSystemOptions() {
      getSystemDictInteg().then((res) => {
        this.systemOptions = res.data;
      });
    },
    /** 查询后台集成用户列表 */
    getList() {
      this.loading = true;
      listInteg(this.queryParams).then((response) => {
        this.integList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        guid: null,
        account: null,
        remark: null,
        systemName: null,
        userId: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        userName: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.guid);
      this.accounts = selection.map((item) => item.account);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      console.log(this.$refs.chooseUserData.ids);
      console.log(this.$refs.chooseUserData.names);
      this.reset();
      this.open = true;
      this.title = "添加后台集成用户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const guid = row.guid || this.ids;
      getInteg(guid).then((response) => {
        this.form = response.data;
        this.userId = this.form.userId;
        this.userName = this.form.userName;
        this.open = true;
        this.title = "修改后台集成用户";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.guid != null) {
            const reqParams = this.form;
            updateInteg(reqParams).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            const reqParams = this.form;
            addInteg(reqParams).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const guids = row.guid || this.ids;
      const accounts = row.account || this.accounts;
      this.$confirm(
        '是否确认删除后台集成用户账号为"' + accounts + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delInteg(guids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有后台集成用户数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return exportInteg(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
        });
    },
    tableSortChange(column) {
      this.queryParams.pageNum = 1;
      if (column.order === "descending") {
        this.queryParams.sortBy = column.prop;
        this.queryParams.order = "desc";
      } else {
        this.queryParams.sortBy = column.prop;
        this.queryParams.order = "asc";
      }
      this.getList();
    },
     statusFormat(row, column) {
       let status = row.status;
       let formatName = '';
       if(status == 0){
          formatName = "启用";
       }else if(status == 1){
          formatName = "停用";
       }
      return formatName;
    },
     flagFormat(row, column) {
       let flag = row.flag;
       let formatName = '';
       if(flag == 0){
          formatName = "否";
       }else if(flag==1){
          formatName = "是";
       }
      return formatName;
    },
  },
};
</script>
